import * as types from './actionTypes';
import axios from 'axios';
import {fromJS} from 'immutable';


const getHomeData = (data) => ({
    type: types.GET_HOME_DATA,
    topicList: fromJS(data.topicList),
    articleList: fromJS(data.articleList),
    recommendPicList: fromJS(data.recommendPicList),
    qrcode: fromJS(data.qrcode)
});

const getMoreListData = (data,newPage,hasMore) => ({
    type: types.GET_MORE_LIST_DATA,
    data: fromJS(data),
    newPage,
    hasMore
});

export const asyncMoreListData = (page) => {
    return (dispatch) => {
        axios.get("/api/home/home.json").then(res => {
            if (res.data.code === 200) {
                const data = res.data.data;
                const maxListLength = 6;
                let _newPage = page + 1;
                let _hasMore = true;
                if (data.moreList.length < maxListLength) {
                    _hasMore = false;
                    dispatch(getMoreListData(data,_newPage,_hasMore));
                    return;
                }
                dispatch(getMoreListData(data,_newPage,_hasMore));
            } else {
                console.log(12);
            }
        })
        .catch(() => {
            console.log('出错了')
        })
    }
};


export const asyncHomeData = () => {
    return (dispatch) => {
        axios.get("/api/home/moreList.json").then(res => {
            if (res.data.code === 200) {
                const data = res.data.data;
                dispatch(getHomeData(data))
            } else {
                console.log(12);
            }
        })
            .catch(() => {
                console.log('出错了')
            })
    }
};